package com.hfuu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfuu.dao.read.custom.TbCheckRecordReadDao;
import com.hfuu.dao.read.custom.TbUserReadDao;
import com.hfuu.dao.write.custom.TbActivityWriteDao;
import com.hfuu.dao.write.custom.TbCheckRecordWriteDao;
import com.hfuu.dao.write.custom.TbUserWriteDao;
import com.hfuu.enums.system.ExceptionEnum;
import com.hfuu.exceptions.ServiceExceptionSpec;
import com.hfuu.model.po.TbActivity;
import com.hfuu.model.po.TbActivityExample;
import com.hfuu.model.po.TbCheckRecord;
import com.hfuu.model.po.TbUser;
import com.hfuu.service.check.SchoolCheckService;
import com.hfuu.service.check.request.SchoolCheckRequest;
import com.hfuu.service.check.request.UpdateCheckRequest;
import com.hfuu.service.check.response.SchoolCheckResponse;
import com.hfuu.utils.MailUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: admin
 * Date: 2018/4/27
 * time: 上午11:45
 */
@Service
public class SchoolCheckServiceImpl implements SchoolCheckService {

    @Autowired
    private TbCheckRecordReadDao tbCheckRecordReadDao;

    @Autowired
    private TbCheckRecordWriteDao tbCheckRecordWriteDao;

    @Autowired
    private TbUserReadDao tbUserReadDao;

    @Autowired
    private TbUserWriteDao tbUserWriteDao;

    @Autowired
    private TbActivityWriteDao tbActivityWriteDao;

    @Override
    public PageInfo listCheckData(SchoolCheckRequest request) throws ServiceExceptionSpec {

        if (request.getPageNo() == null ||
                request.getPageSize() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        if (StringUtils.isNotBlank(request.getStartTime())) {
            request.setStartTime(request.getStartTime() + " 00:00:00");
        }
        if (StringUtils.isNotBlank(request.getEndTime())) {
            request.setEndTime(request.getEndTime() + " 23:59:59");
        }
        if (StringUtils.isEmpty(request.getEndTime())) {
            request.setEndTime(null);
        }
        if (StringUtils.isEmpty(request.getStartTime())) {
            request.setStartTime(null);
        }
        request.setType(request.getDataType());
        PageHelper.startPage(request.getPageNo(), request.getPageSize());
        List<SchoolCheckResponse> rList = tbCheckRecordReadDao.listCheckData(request.getUserName(),
                request.getStartTime(), request.getEndTime(), request.getType(), request.getStatus(), request.getResult());

        PageInfo pageInfo = new PageInfo(rList);
        return pageInfo;
    }

    @Override
    public TbCheckRecord getCheckDataDetail(SchoolCheckRequest request) throws ServiceExceptionSpec {

        if (request.getId() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }

        TbCheckRecord tbCheckRecord = tbCheckRecordReadDao.selectByPrimaryKey(request.getId());
        return tbCheckRecord == null ? new TbCheckRecord() : tbCheckRecord;
    }

    @Override
    public Integer updateCheckData(UpdateCheckRequest request) throws ServiceExceptionSpec {

        if (request.getId() == null || request.getUserId() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }

        //发送邮件给用户，先获得用户邮箱
        TbUser tbUser = tbUserReadDao.selectByPrimaryKey(request.getUserId());
        if (tbUser == null || StringUtils.isEmpty(tbUser.getEmail())) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_EMAIL_ERROR);
        }

        //先更新审核结果
        TbCheckRecord record = new TbCheckRecord();
        record.setFinish(2);
        record.setRemark(request.getRemark());
        record.setId(request.getId());
        record.setResult(request.getResult());
        int total = tbCheckRecordWriteDao.updateByPrimaryKeySelective(record);

        //判断募捐审核
        TbCheckRecord tbCheckRecord = tbCheckRecordReadDao.selectByPrimaryKey(request.getId());

        String result = "";
        if (request.getResult() == 2) {
            result = "尊敬的组织申请者，非常抱歉您的资料未通过审核，原因是：" + request.getRemark() + "。感谢您的注册申请！";
            if (tbCheckRecord.getType().equals(2)) {
                result = "尊敬的组织管理员，非常抱歉您的资料未通过审核，原因是：" + request.getRemark() + "。";
            }
        } else if (request.getResult() == 1) {
            result = "尊敬的组织申请者，恭喜您通过审核！" ;
            if (tbCheckRecord.getType().equals(2)) {
                result = "尊敬的组织管理员，恭喜您的募捐活动通过审核。";
            }
            TbActivity tbActivity = new TbActivity();
            tbActivity.setDisabled(0);
            TbActivityExample example = new TbActivityExample();
            TbActivityExample.Criteria criteria = example.createCriteria();
            criteria.andDisabledEqualTo(1).andOrganizerEqualTo(tbUser.getId());
            tbActivityWriteDao.updateByExampleSelective(tbActivity, example);
            tbUser.setStatus(1);
            tbUserWriteDao.updateByPrimaryKeySelective(tbUser);
        }

        String[] array = new String[] {tbUser.getEmail()};
        String subject = "组织注册申请结果";
        String html = "<html><head>"+
                "</head><body>"+
                "<h1>注册结果通知!</h1>"+
                "<span style='color:red;font-size:36px;'>" + result + "</span>"+
                "<img src='cid:javaxmail.png'>"+
                "</body></html>";
        if (!MailUtil.htmlMail(array, subject, html)) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_EMAIL_ERROR);
        }

        return total;
    }
}
